home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / timer.h < prev    next >
C/C++ Source or Header  |  1999-03-14  |  3KB  |  104 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- // 
  5. // C++ Header File Name: timer.cpp
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer 
  8. // File Creation Date: 12/11/1996 
  9. // Date Last Modified: 03/15/1999
  10. // Copyright (c) 1997 Douglas M. Gaer
  11. // ----------------------------------------------------------- // 
  12. // ---------- Include File Description and Details  ---------- // 
  13. // ----------------------------------------------------------- // 
  14. /*
  15. The VBD C++ classes are copyright (c) 1997, by Douglas M. Gaer.
  16. All those who put this code or its derivatives in a commercial
  17. product MUST mention this copyright in their documentation for
  18. users of the products in which this code or its derivative
  19. classes are used. Otherwise, you have the freedom to redistribute
  20. verbatim copies of this source code, adapt it to your specific
  21. needs, or improve the code and release your improvements to the
  22. public provided that the modified files carry prominent notices
  23. stating that you changed the files and the date of any change.
  24.  
  25. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  26. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  27. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  28. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  29. CORRECTION.
  30.  
  31. The Sleep() function is used to pause the program for a given
  32. number of seconds. The Start(), Stop(), and ElapsedSeconds()
  33. functions are used to calculate elapsed CPU cycles in seconds.
  34. */
  35. // ----------------------------------------------------------- // 
  36. #ifndef __TIMER_HPP
  37. #define __TIMER_HPP
  38.  
  39. #include <time.h>
  40.  
  41. // Sleep for specified number of seconds.
  42. inline void Sleep(clock_t seconds)
  43. {
  44.   // Convert milliseconds to seconds
  45.   clock_t wait = ( seconds * CLOCKS_PER_SEC );
  46.  
  47.   clock_t goal = wait + clock();
  48.   while( goal > clock() )
  49.     ;
  50. }
  51.  
  52. // Sleep for specified number of seconds.
  53. // Same as Sleep(), added to prevent name
  54. // conflicts with other functions aready
  55. // named Sleep().
  56. inline void SleepFor(clock_t seconds)
  57. {
  58.   // Convert milliseconds to seconds
  59.   clock_t wait = ( seconds * CLOCKS_PER_SEC );
  60.   clock_t goal = wait + clock();
  61.   while( goal > clock() )
  62.     ;
  63. }
  64.  
  65. // Pauses for a specified number of milliseconds. 
  66. inline void PauseFor(clock_t wait)
  67. {
  68.   clock_t goal;
  69.   goal = wait + clock();
  70.   while( goal > clock() )
  71.     ;
  72. }
  73.  
  74. // Mark the starting time for the routine.
  75. inline clock_t Start()
  76. {
  77.   return clock();
  78. }
  79.  
  80. // Mark the stop time for the routine. 
  81. inline clock_t Stop()
  82. {
  83.   return clock();
  84. }
  85.  
  86. // Calculate the elapsed time in seconds.
  87. inline clock_t ElapsedSeconds(clock_t begin, clock_t end)
  88. {
  89.   return (end - begin) / CLOCKS_PER_SEC;
  90. }
  91.  
  92. // Calculate the elapsed time in milliseconds. 
  93. inline double ElapsedTime(clock_t begin, clock_t end)
  94. {
  95.   return (double)(end - begin) / CLOCKS_PER_SEC;
  96. }
  97.  
  98. #endif // __TIMER_HPP
  99. // ----------------------------------------------------------- // 
  100. // ------------------------------- //
  101. // --------- End of File --------- //
  102. // ------------------------------- //
  103.  
  104.